﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompanyBussiness
{
    public class NewsManagement :INewsManagement
    {
        private CompanyDataContext companydata = new CompanyDataContext();

        public virtual List<Category> GetParentCategory(int languagesID, Boolean categoryStatus, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.LanguagesID == languagesID && ca.ParentCategoryID == null && ca.CategoryStatus == categoryStatus
                            && ca.ModulCode == modulCode
                           select ca).ToList();
            return category;
        }

        public virtual List<Category> GetParentCategoryAll(int languagesID, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.LanguagesID == languagesID && ca.ParentCategoryID == null
                            && ca.ModulCode == modulCode
                            select ca).ToList();
            return category;
        }

        public virtual List<Category> GetChildrenCategoryAll(int languagesID, Boolean categoryStatus, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.LanguagesID == languagesID && ca.CategoryStatus == categoryStatus && ca.ParentCategoryID != null
                            && ca.ModulCode == modulCode
                            select ca).ToList();
            return category;
        }

        public virtual List<Category> GetChildrenCategoryByParentID(int parentCategoryID, int languagesID, Boolean categoryStatus, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.LanguagesID == languagesID && ca.ParentCategoryID == parentCategoryID && ca.CategoryStatus == categoryStatus
                            && ca.ModulCode == modulCode
                            select ca);
            return category.ToList();
        }

        public virtual List<Category> GetChildrenCategoryByParentIDAll(int parentCategoryID, int languagesID, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.LanguagesID == languagesID && ca.ParentCategoryID == parentCategoryID
                            && ca.ModulCode == modulCode
                            select ca);
            return category.ToList();
        }

        public virtual Category GetCategoryByID(int categoryID, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.CategoryID == categoryID
                            && ca.ModulCode == modulCode
                           select ca).FirstOrDefault();
            return category;
        }

        public virtual Category GetCategoryByNameSingle(string categoryName, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.CategoryName == categoryName
                            && ca.ModulCode == modulCode
                            select ca).FirstOrDefault();
            return category;
        }

        public virtual Category GetCategoryByParentCategoryID(int categoryID, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.CategoryID == categoryID
                            && ca.ModulCode == modulCode
                            select ca).Single();
            if (category.ParentCategoryID != null)
            {
                var parent = (from pe in companydata.Categories
                              where pe.CategoryID == category.ParentCategoryID
                              select pe).Single();
                return parent;
            }
            else
                return null;
            
        }

        public virtual List<Category> GetCategoryByName(string categoryName, int languagesID, Boolean categoryStatus, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.CategoryName.Contains(categoryName) == true && ca.LanguagesID == languagesID && ca.CategoryStatus == categoryStatus
                            && ca.ModulCode == modulCode
                            select ca).ToList();
            return category;
        }
        public virtual List<Category> GetCategoryByStatus(int languagesID, Boolean categoryStatus, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.CategoryStatus == categoryStatus && ca.LanguagesID==languagesID
                            && ca.ModulCode == modulCode
                            select ca).ToList();
            return category;
        }


        public virtual int GetTopParentCategoryID(int languagesID, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.ParentCategoryID == null && ca.LanguagesID==languagesID && ca.ModulCode==modulCode
                            orderby ca.CategoryID ascending
                            select ca).FirstOrDefault();

            return category !=null ? category.CategoryID : 0;
        }


        public virtual List<New> GetNewsByCategoryStatus(int categoryID, Boolean newsStatus, int take, int StartRow,string modulcode)
        {
            var cate = (from ca in companydata.Categories
                        where ca.ModulCode == modulcode
                        select ca.CategoryID.ToString()).ToArray();

            var news = (from ne in companydata.News
                        where ne.NewsStatus == newsStatus
                        select ne); //co phan trang moi lan lay take tin theo danh muc tin

            //foreach (var t in cate)
            //{
            //    news = news.Where(p => p.CategoryID == t.CategoryID);
            //}

            string[] arr = cate;
            news = news.Where(p => arr.Contains(p.CategoryID.ToString()));


            if (categoryID != 0)
            {
                news = (from p in news
                        where p.CategoryID == categoryID
                        select p);
            }

            news = (from p in news
                    orderby p.NewsID descending
                    select p);

            if (news.Count() < take)
                return news.ToList();
            else
                return news.Skip(StartRow).Take(take).ToList();
        }

        public virtual int GetNewsByCategoryStatusPageCount(int categoryID, Boolean newsStatus, int take, int StartRow, string modulcode)
        {
            var cate = (from ca in companydata.Categories
                        where ca.ModulCode == modulcode
                        select ca.CategoryID.ToString()).ToArray();

            var news = (from ne in companydata.News
                        where ne.NewsStatus == newsStatus
                        select ne); //co phan trang moi lan lay take tin theo danh muc tin

            //foreach (var t in cate)
            //{
            //    news = news.Where(p => p.CategoryID == t.CategoryID);
            //}

            string[] arr = cate;
            news = news.Where(p => arr.Contains(p.CategoryID.ToString()));

            if (categoryID != 0)
            {
                news = (from p in news
                        where p.CategoryID == categoryID
                        select p);
            }

            news = (from p in news
                    orderby p.NewsID descending
                    select p);

            return news.Count();
        }

        public virtual List<New> GetNewsByParentCategory(int categoryID, int take, int StartRow)
        {
            var news = (from ne in companydata.News
                        join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                        where ne.CategoryID == categoryID || ca.ParentCategoryID==categoryID
                        orderby ne.NewsID descending
                        select ne); //co phan trang moi lan lay take tin theo danh muc tin
            if (news.Count() < take)
                return news.ToList();
            else
                return news.Skip(StartRow).Take(take).ToList();
        }
        public virtual List<New> GetNewsByCategory(int categoryID, int take, int StartRow)
        {
            var news = (from ne in companydata.News
                        where ne.CategoryID == categoryID
                        orderby ne.NewsID descending
                        select ne); //co phan trang moi lan lay take tin theo danh muc tin
            if (news.Count() < take)
                return news.ToList();
            else
                return news.Skip(StartRow).Take(take).ToList();
        }

        public virtual List<New> GetNewsSearchNotDate(New news, int take, int StartRow)
        {
            var cate = (from p in companydata.Categories
                        where p.CategoryID == news.CategoryID || p.ParentCategoryID == news.CategoryID
                        select p.CategoryID.ToString()).ToArray();

            var sNews = (from ne in companydata.News
                         //orderby ne.NewsID descending
                         select ne);

            string[] arr = cate;
            sNews = sNews.Where(p => arr.Contains(p.CategoryID.ToString()));

            if (news.NewsSubject != String.Empty)
            {
                sNews = (from ne in sNews
                         where ne.NewsSubject.Contains(news.NewsSubject) == true
                         select ne);
            }

            if (news.NewsStatus != null)
            {
                sNews = (from ne in sNews
                         where ne.NewsStatus == news.NewsStatus
                         select ne);
            }

            sNews = (from p in sNews
                     orderby p.NewsID descending
                     select p);

            if (sNews.Count() < take)
                return sNews.ToList();
            else
                return sNews.Skip(StartRow).Take(take).ToList();
        
        }

        public virtual List<New> GetNewsSearch(New news, DateTime DateForm, DateTime DateTo, int take, int StartRow)
        {
            var sNews = (from ne in companydata.News
                         join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                         where (ne.CategoryID == news.CategoryID || ca.ParentCategoryID == news.CategoryID) //&& (ne.NewsDateCreate >= DateForm) && (ne.NewsDateCreate <= DateTo)
                         orderby ne.NewsID descending
                         select ne);
            if (news.NewsSubject != String.Empty)
            {
                sNews =(from ne in sNews
                        where ne.NewsSubject.Contains(news.NewsSubject)==true 
                        select ne);
            }

            if (news.NewsStatus != null)
            {
                sNews = (from ne in sNews
                         where ne.NewsStatus == news.NewsStatus
                         select ne);
            }

            sNews = (from p in sNews
                     where p.NewsDateCreate >= DateForm && p.NewsDateCreate <= DateTo
                     select p);
            //sNews = sNews.Where(p => p.NewsDateCreate >= DateForm && p.NewsDateCreate <= DateTo);


            if (sNews.Count() < take)
                return sNews.ToList();
            else
                return sNews.Skip(StartRow).Take(take).ToList();
           
        }
        public virtual int GetNumberNewsByCategory(int categoryID)
        {
            var num = (from ne in companydata.News
                        join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                        where ne.CategoryID == categoryID || ca.ParentCategoryID==categoryID
                        orderby ne.NewsID descending
                        select ne).Count();
            //var num = (from ne in companydata.News
            //           where ne.CategoryID == categoryID
            //           select ne).Count();
            return num;
        }

        public virtual int GetNumberNewsSearchNotDate(New news)
        {
            var sNews = (from ne in companydata.News
                         join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                         where (ne.CategoryID == news.CategoryID || ca.ParentCategoryID == news.CategoryID) 
                         orderby ne.NewsID descending
                         select ne);
            if (news.NewsSubject != String.Empty)
            {
                sNews = (from ne in sNews
                         where ne.NewsSubject.Contains(news.NewsSubject) == true
                         select ne);
            }

            if (news.NewsStatus != null)
            {
                sNews = (from ne in sNews
                         where ne.NewsStatus == news.NewsStatus
                         select ne);
            }

            return sNews.Count();
        }

        public virtual int GetNumberNewsSearch(New news, DateTime DateForm, DateTime DateTo)
        {
            var sNews = (from ne in companydata.News
                         join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                         where (ne.CategoryID == news.CategoryID || ca.ParentCategoryID == news.CategoryID) //&& (ne.NewsDateCreate >= DateForm) && (ne.NewsDateCreate <= DateTo)
                         orderby ne.NewsID descending
                         select ne);
            if (news.NewsSubject != String.Empty)
            {
                sNews = (from ne in sNews
                         where ne.NewsSubject.Contains(news.NewsSubject) == true
                         select ne);
            }

            if (news.NewsStatus != null)
            {
                sNews = (from ne in sNews
                         where ne.NewsStatus == news.NewsStatus
                         select ne);
            }
            sNews = (from p in sNews
                     where p.NewsDateCreate >= DateForm && p.NewsDateCreate <= DateTo
                     select p);
            //sNews = sNews.Where(p => p.NewsDateCreate >= DateForm && p.NewsDateCreate <= DateTo);

            return sNews.Count();
        }

        public virtual List<New> GetNewsByCategoryIDAll(int categoryID)
        {
            var news = (from ne in companydata.News
                        where ne.CategoryID == categoryID
                        orderby ne.NewsID descending
                        select ne); 
            return news.ToList();
        }
        public virtual New GetNewsByID(int newsID)
        {
            var news = (from ne in companydata.News
                        where ne.NewsID == newsID
                        orderby ne.NewsID descending
                        select ne).Single();
            return news;
        }
        public virtual List<New> GetNewsByTitle(string newsSubject, int take, int StartRow)
        {
            var news = (from ne in companydata.News
                        where ne.NewsSubject.Contains(newsSubject) == true
                        orderby ne.NewsID descending
                        select ne); //co phan trang moi lan lay take tin (tim kiem tin theo tieu de)
            if (news.Count() < take)
                return news.ToList();
            else
                return news.Skip(StartRow).Take(take).ToList();
        }
        public virtual List<New> GetNewsByDate(int languagesID, DateTime newsDate, int take, int StartRow)
        {
            var news = (from ne in companydata.News
                        join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                        where ne.NewsDate==newsDate && ca.LanguagesID==languagesID
                        orderby ne.NewsID descending
                        select ne); //co phan trang moi lan lay take tin (tim kiem tin theo ngay)
            if(news.Count()<take)
                return news.ToList();
            else
                return news.Skip(StartRow).Take(take).ToList();
        }
        public virtual List<New> GetNewsByStatus(int languagesID, Boolean newsStatus, int take, int StartRow)
        {
            var news = (from ne in companydata.News
                        join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                        where ne.NewsStatus == newsStatus && ca.LanguagesID==languagesID
                        orderby ne.NewsID descending
                        select ne); //co phan trang moi lan lay take tin (tim kiem tin theo trang thai)
            if (news.Count() < take)
                return news.ToList();
            else
                return news.Skip(StartRow).Take(take).ToList();
        }

        public virtual Array GetListNewsRandom(int categoryID, Boolean newsStatus)
        {
            var news = (from p in companydata.News
                        where p.CategoryID == categoryID && p.NewsStatus == newsStatus
                        orderby p.NewsID descending
                        select p);
            int rows=news !=null ? news.Count() : 0;
            //Array arr = new Array[10];
            New[] arr;
            if (rows > 10)
            {
                arr = new New[10];
                for (int i = 0; i < 10; i++)
                {
                    arr[i] = news.Where(p => p.NewsID == new Random(rows).Next() && p.NewsID>10).SingleOrDefault();
                }
                return arr;
            }
            else
                return null;
            
           
            

        }


        public virtual int CreateCategory(int parentCategoryID, string categoryName, string categoryIntro, string categoryDes, string categoryImg, Boolean categoryStatus,Boolean imagesStatus, int languagesID, string modulCode)
        {
            Category category = new Category();
            if (parentCategoryID == 0)
            {
                category.ParentCategoryID = null;
            }
            else
            {
                category.ParentCategoryID = parentCategoryID;
            }
            
            
            category.CategoryName = categoryName;
            category.CategoryIntro = categoryIntro;
            category.CategoryDes = categoryDes;
            category.CategoryImg = categoryImg;
            category.CategoryStatus = categoryStatus;
            category.LanguagesID = languagesID;
            category.ModulCode = modulCode;
            category.ImagesStatus = imagesStatus;

            companydata.Categories.InsertOnSubmit(category);
            companydata.SubmitChanges();

            return category.CategoryID;
        }

        public virtual void UpdateCategory(int categoryID, int parentCategoryID, string categoryName, string categoryIntro, string categoryDes, string categoryImg, Boolean categoryStatus, Boolean imagesStatus, int languagesID, string modulCode)
        {
            var category = (from ca in companydata.Categories
                            where ca.CategoryID==categoryID
                            select ca).Single();

            if (parentCategoryID == 0)
            {
                category.ParentCategoryID = null;
            }
            else
            {
                category.ParentCategoryID = parentCategoryID;
            }
            category.CategoryName = categoryName;
            category.CategoryIntro = categoryIntro;
            category.CategoryDes = categoryDes;
            category.CategoryImg = categoryImg;
            category.CategoryStatus = categoryStatus;
            category.LanguagesID = languagesID;
            category.ModulCode = modulCode;
            category.ImagesStatus = imagesStatus;

            companydata.SubmitChanges();
        }

        public virtual void DeleteCategory(int categoryID)
        {
            var category = (from ca in companydata.Categories
                            where ca.CategoryID==categoryID
                            select ca).Single();

            companydata.Categories.DeleteOnSubmit(category);
            companydata.SubmitChanges();
        }


        public virtual int CreateNews(string newsSubject, string newsIntro, string newsContent, string newsAuthor, string newsSource, string newsLink, DateTime newsDateCreate, int newsAccess, string newsImage, Boolean newsStatus, string fileAttach, Boolean imagesStatus, int categoryID)
        {
            New news = new New();
            news.NewsSubject = newsSubject;
            news.NewsIntro = newsIntro;
            news.NewsContent = newsContent;
            news.NewsAuthor = newsAuthor;
            news.NewsSource = newsSource;
            news.NewsLink = newsLink;
            news.NewsDateCreate = newsDateCreate;
            //news.NewsDate = newsDate;
            news.NewsAccess = newsAccess;
            news.NewsImage = newsImage;
            news.NewsStatus = newsStatus;
            news.CategoryID = categoryID;
            news.FileAttach = fileAttach;
            news.ImagesStatus = imagesStatus;

            companydata.News.InsertOnSubmit(news);
            companydata.SubmitChanges();

            return news.NewsID;
        }

        public virtual void UpdateNews(int newsID, string newsSubject, string newsIntro, string newsContent, string newsAuthor, string newsSource, string newsLink, DateTime newsDate, int newsAccess, string newsImage, Boolean newsStatus, string fileAttach, Boolean imagesStatus, int categoryID)
        { 
            var news=(from ne in companydata.News
                      where ne.NewsID==newsID
                      select ne).Single();

            news.NewsSubject = newsSubject;
            news.NewsIntro = newsIntro;
            news.NewsContent = newsContent;
            news.NewsAuthor = newsAuthor;
            news.NewsSource = newsSource;
            news.NewsLink = newsLink;
            //news.NewsDateCreate = newsDateCreate;
            news.NewsDate = newsDate;
            news.NewsAccess = newsAccess;
            news.NewsImage = newsImage;
            news.NewsStatus = newsStatus;
            news.CategoryID = categoryID;
            news.FileAttach = fileAttach;
            news.ImagesStatus = imagesStatus;

            companydata.SubmitChanges();
        }

        public virtual void DeleteNews(int newsID)
        {
            var news = (from ne in companydata.News
                        where ne.NewsID == newsID
                        select ne).Single();

            companydata.News.DeleteOnSubmit(news);
            companydata.SubmitChanges();
        }


        public virtual Metadata GetMetadataByNewsID(int newsID)
        {
            var meta = (from p in companydata.Metadatas
                        where p.NewsID == newsID
                        select p).FirstOrDefault();
            return meta;
        }
        public virtual Metadata GetMetadataByProductID(int productID)
        {
            var meta = (from p in companydata.Metadatas
                        where p.ProductID == productID
                        select p).FirstOrDefault();
            return meta;
        }
        public virtual Metadata GetMetadataByCategoryID(int categoryID)
        {
            var meta = (from p in companydata.Metadatas
                        where p.CategoryID == categoryID
                        select p).FirstOrDefault();
            return meta;
        }
        public virtual Metadata GetMetadataByTypeProductID(int typeProductID)
        {
            var meta = (from p in companydata.Metadatas
                        where p.TypeProductID == typeProductID
                        select p).FirstOrDefault();
            return meta;
        }

        public virtual void CreateMetadata(Metadata metadata)
        {
            Metadata meta = new Metadata();
            meta.MetaKeyword = metadata.MetaKeyword;
            meta.MetaDesciption = metadata.MetaDesciption;
            meta.MetaCode = metadata.MetaCode;
            meta.NewsID = metadata.NewsID;
            meta.ProductID = metadata.ProductID;
            meta.Title = metadata.Title;
            meta.CategoryID = metadata.CategoryID;
            meta.TypeProductID = metadata.TypeProductID;

            companydata.Metadatas.InsertOnSubmit(meta);
            companydata.SubmitChanges();
        }
        public virtual void UpdateMetadata(Metadata metadata)
        {
            Metadata meta;

            if (metadata.CategoryID != null)
            {
                meta = (from p in companydata.Metadatas
                        where p.CategoryID == metadata.CategoryID
                        select p).Single();

                meta.MetaKeyword = metadata.MetaKeyword;
                meta.MetaDesciption = metadata.MetaDesciption;
                meta.MetaCode = metadata.MetaCode;
                meta.NewsID = metadata.NewsID;
                meta.ProductID = metadata.ProductID;
                meta.Title = metadata.Title;
                meta.CategoryID = metadata.CategoryID;
                meta.TypeProductID = metadata.TypeProductID;

                companydata.SubmitChanges();
            }
            else if (metadata.TypeProductID != null)
            {
                meta = (from p in companydata.Metadatas
                        where p.TypeProductID == metadata.TypeProductID
                        select p).Single();

                meta.MetaKeyword = metadata.MetaKeyword;
                meta.MetaDesciption = metadata.MetaDesciption;
                meta.MetaCode = metadata.MetaCode;
                meta.NewsID = metadata.NewsID;
                meta.ProductID = metadata.ProductID;
                meta.Title = metadata.Title;
                meta.CategoryID = metadata.CategoryID;
                meta.TypeProductID = metadata.TypeProductID;

                companydata.SubmitChanges();
            }
            else if (metadata.NewsID != null)
            {
                meta = (from p in companydata.Metadatas
                        where p.NewsID == metadata.NewsID
                        select p).Single();

                meta.MetaKeyword = metadata.MetaKeyword;
                meta.MetaDesciption = metadata.MetaDesciption;
                meta.MetaCode = metadata.MetaCode;
                meta.NewsID = metadata.NewsID;
                meta.ProductID = metadata.ProductID;
                meta.Title = metadata.Title;
                meta.CategoryID = metadata.CategoryID;
                meta.TypeProductID = metadata.TypeProductID;

                companydata.SubmitChanges();
            }
            else if (metadata.ProductID != null)
            {
                meta = (from p in companydata.Metadatas
                        where p.ProductID == metadata.ProductID
                        select p).Single();

                meta.MetaKeyword = metadata.MetaKeyword;
                meta.MetaDesciption = metadata.MetaDesciption;
                meta.MetaCode = metadata.MetaCode;
                meta.NewsID = metadata.NewsID;
                meta.ProductID = metadata.ProductID;
                meta.Title = metadata.Title;
                meta.CategoryID = metadata.CategoryID;
                meta.TypeProductID = metadata.TypeProductID;

                companydata.SubmitChanges();
            }

            
        }
        public virtual void DeletaMetadata(int metaID)
        {
            var meta = (from p in companydata.Metadatas
                        where p.ProductID == metaID
                        select p).FirstOrDefault();

            companydata.Metadatas.DeleteOnSubmit(meta);
            companydata.SubmitChanges();
        }

        public virtual Modul GetModulByModulCode(string modulCode)
        {
            var modul=(from p in companydata.Moduls
                           where p.ModulCode==modulCode
                           select p).FirstOrDefault();
            return modul;
        }

        public virtual New GetNewsOneByCategoryName(string categoryName, int languagesID)
        { 
             var news = (from ne in companydata.News
                        join ca in companydata.Categories on ne.CategoryID equals ca.CategoryID
                        where ca.CategoryName==categoryName && ca.LanguagesID==languagesID
                        orderby ne.NewsID descending
                        select ne).FirstOrDefault(); 
            return news;
             
        }

        public virtual List<New> GetListNewsSearch(string newSubject, int take, int StartRow)
        {
            var news = (from ne in companydata.News
                        where ne.NewsSubject.Contains(newSubject) == true
                        select ne);
            return news!=null ? news.Skip(StartRow).Take(take).ToList() : null;
        }

        public virtual string GetNameCateByID(int id)
        {
            var cate = (from ca in companydata.Categories
                        where ca.CategoryID == id
                        select ca).FirstOrDefault();
            if (cate == null)
                return "";
            else
                return cate.CategoryName;
        }

        public virtual string GetNameNewsByID(int id)
        {
            var news = (from ne in companydata.News
                        where ne.NewsID == id
                        select ne).FirstOrDefault();
            if (news == null)
                return "";
            else
                return news.NewsSubject;
        }
    }
}
